<template>
  <el-container class="container">
    <el-header class="header">
      <el-row>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <img src="../../assets/logo.png" alt="无法显示图片" style="object-fit:contain" />
          </div>
        </el-col>
        <el-col :span="18" class="middle">
          <h3>电商后台管理系统</h3>
        </el-col>
        <el-col :span="2">
          <div class="loginout" @click.prevent="handleSignout()">
            <!-- <a class="loginout" @click.prevent="handleSignout()" href="#">退出</a> -->
            <el-button type="info">退出</el-button>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <el-aside class="aside" width="200px">
        <!-- 侧边栏导航 -->
        <el-menu :router="true" :unique-opened="true">
          <el-submenu :index="''+item1.order" v-for="(item1, index) in menus" :key="index">
            <template slot="title">
                <i class="el-icon-user-solid"></i>
                <span>{{ item1.authName }}</span>
            </template>
              <el-menu-item :index="item2.path" v-for="(item2, index) in item1.children" :key="index">
                <i class="el-icon-s-grid"></i>
                <span>{{ item2.authName }}</span>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return {
      menus:[]
    }
  },
  // new vue之前自动触发
  beforeCreate () {
    const token = localStorage.getItem('token')
    // 获取token
    if (!token) {
      // 如果没有token则跳转到登录
      this.$router.push({name: 'login'})
    }
    // 如果有token则继续渲染组件
  },
  created(){
      this.getMenus()
    },
  methods: {
    handleSignout () {
      // 1.清除token
      localStorage.clear()
      // 2.提示退出成功
      this.$message.success('退出成功')
      // 3.回到Login组件
      this.$router.push({name: 'login'})
    },
    // 获取导航权限数据
    async getMenus(){
      const res = await this.$http.get('menus')
      this.menus = res.data.data
      console.log(this.menus)
    }
  }
}
</script>

<style>
.container {
  height: 100%;
}

.header {
  background-color: #b3c0d1;
}

.aside {
  background-color: #d3dce6;
}

.main {
  background-color: #e9eef3;
  height: 1000px;
}

/* 头部样式 */
.middle {
  text-align: center;
}

.loginout {
  line-height: 60px;
  text-align: center;
  text-decoration: none;
}
</style>
